import type { RouteRecordRaw } from 'vue-router';

/**
 * @function loadLocalRoutes 读取本地的路由配置文件
 */
function loadLocalRoutes() {
  const routeList: RouteRecordRaw[] = [];
  // 6.1获取到文件对象
  const files: Record<string, any> = import.meta.glob('/src/router/main/**/*.ts', {
    eager: true
  });
  // 6.2遍历文件对象
  for (const key in files) {
    const module = files[key];
    routeList.push(module.default);
  }
  return routeList;
}

export let firstMenu: any = null;

/**
 * @function mapMenusToRoutes 将菜单属于映射为路由，用于动态加载路由
 * @param userMenus 用户菜单，通常是来自localStorage或者请求的菜单的数据
 */
export function mapMenusToRoutes(userMenus: any[]) {
  const localRoutes: RouteRecordRaw[] = loadLocalRoutes();
  const routes: RouteRecordRaw[] = [];

  for (const menu of userMenus) {
    for (const subMenu of menu.children) {
      const route = localRoutes.find((item) => item.path === subMenu.url);
      if (route) {
        // 为一级菜单添加重定向，增加面包屑导航的使用体验
        if (!userMenus.find((item) => item.path === menu.url)) {
          routes.push({ path: menu.url, redirect: route.path });
        }
        // 将二级菜单添加到路由
        routes.push(route);
      }
      if (!firstMenu && subMenu) firstMenu = subMenu;
    }
  }
  return routes;
}

interface IMenu {
  children: any;
  id: number;
  name: string;
  parentId: number;
  sort: number;
  type: number;
  url: string;
}

/**
 * @function mapPathToMenu 将路由映射为菜单
 * @param path
 * @param userMenus
 */
export function mapPathToMenu(path: string, userMenus: any[]): IMenu | undefined {
  for (const menu of userMenus) {
    for (const subMenu of menu.children) {
      if (subMenu.url === path) return subMenu;
    }
  }
}

interface IBreadcrumb {
  name: string;
  path: string;
}

/**
 * @function mapPathToBreadcrumb 将路由映射为面包屑导航
 * @param path 路由
 * @param userMenus 用户登录后获取的菜单列表
 */
export function mapPathToBreadcrumb(path: string, userMenus: any[]) {
  const breadcrumb: IBreadcrumb[] = [];
  for (const menu of userMenus) {
    for (const subMenu of menu.children) {
      if (subMenu.url === path) {
        breadcrumb.push({ name: menu.name, path: menu.url });
        breadcrumb.push({ name: subMenu.name, path: subMenu.url });
      }
    }
  }
  return breadcrumb;
}

/**
 * @function mapRoleObjToArray 将角色对象转换为角色ID组成的数组
 * @param roleObj
 */
export function mapRoleObjToArray(roleObj: any) {
  const roleIds: number[] = [];

  function recurseGetId(menuList: any) {
    for (const item of menuList) {
      if (item.children) {
        recurseGetId(item.children);
      } else {
        roleIds.push(item.id);
      }
    }
  }
  recurseGetId(roleObj);

  return roleIds;
}

/**
 * @function mapMenusToPermissions 将菜单转换为权限
 * @param menus
 */
export function mapMenusToPermissions(menus: any) {
  const permissions: string[] = [];

  function recurseGetPermissions(menuList: any) {
    for (const item of menuList) {
      if (item.type === 3) {
        permissions.push(item.permission);
      } else {
        recurseGetPermissions(item.children ?? []);
      }
    }
  }
  recurseGetPermissions(menus);

  return permissions;
}
